package com.ly.spring;

import com.ly.spring.biz.UserBiz;
import com.ly.spring.mapper.UserMapper;
import com.ly.spring.web.UserAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/11/2
 * Email haijunzhou@hnit.edu.cn
 */
// 注解给 jvm 读取的信息
@Configuration   // beans.xml
@ComponentScan("com.ly.spring")
public class AnnoConfig {

    // <bean class="com.ly.spring.Demo" id="demo"></bean>
    @Bean(initMethod="init", destroyMethod = "destroy")
    @Lazy
    @Scope("prototype")  // 原型模式
    public Demo demo(){
        Demo demo = new Demo();
        demo.setName("张三");
        return demo;
    }

    @Bean(initMethod="init", destroyMethod = "destroy")
    @DependsOn("newDemo")
    public Demo newDemo1(){
        Demo demo = new Demo();
        demo.setName("王五");
        return demo;
    }

    @Bean(initMethod="init", destroyMethod = "destroy")
    @Primary // 默认 bean
    public Demo newDemo(){
        Demo demo = new Demo();
        demo.setName("李四");
        return demo;
    }

    @Bean
    public UserMapper userMapper(){
        return new UserMapper();
    }

    // @Autowired 表示从 IOC 容器中获取该 bean 注入到该属性中
    @Autowired
    UserMapper userMapper;
    @Bean
    public UserBiz userBiz(){
        UserBiz userBiz = new UserBiz();
        userBiz.setUserMapper(userMapper);
        return userBiz;
    }

    // Bean 方法参数对象, Spring 会从IOC容器获取一个对应bean 注入进来
    // 这个叫"接口(方法)注入"
    @Bean
    public UserAction userAction(UserBiz userBiz){
        UserAction userAction = new UserAction();
        userAction.setUserBiz(userBiz);
        return userAction;
    }



}
